Msg("====gSAM initialized...\n")

function gSAM.LoadSettings()
	local pp = gSAM.ServerConfigExists("PP")
	local main = gSAM.ServerConfigExists("Main")
	Msg(pp.." : "..main.."\n")
	if pp then
		gSAM.Settings.PP_Buddys = util.tobool(gSAM.GetServerVar("PP","PP_Buddys")) or true
		gSAM.Settings.PP_AutoRemove = util.tobool(gSAM.GetServerVar("PP","AutoRemove")) or true
		gSAM.Settings.PP_AutoRemoveDelay = gSAM.GetServerVar("PP","AutoRemoveDelay") or 180
	end
	if main then
		gSAM.Settings.PropProtection = util.tobool(gSAM.GetServerVar("Main","PropProtection")) or true
		gSAM.Settings.TimeSpent = util.tobool(gSAM.GetServerVar("Main","TimeSpent")) or true
		gSAM.Settings.Banner = util.tobool(gSAM.GetServerVar("Main","Banner")) or true
		gSAM.Settings.Adverts = util.tobool(gSAM.GetServerVar("Main","Adverts")) or true
		gSAM.Settings.Adverts_Time = gSAM.GetServerVar("Main","Adverts_Time") or 180
		gSAM.Settings.ToolRestriction = util.tobool(gSAM.GetServerVar("Main","ToolRestrict")) or true
		gSAM.Settings.ChatPrefix = gSAM.GetServerVar("Main","ChatPrefix") or "/"
		gSAM.Settings.MOTD = util.tobool(gSAM.GetServerVar("Main","MOTD")) or true
	end
end
hook.Add("Initialize","gSAM.LoadSettings",gSAM.LoadSettings)

function gSAM.PlayerInitSpawn(ply)
	local ftime = false
	if !gSAM.PlayerIsInSystem(ply) then
		ftime = true
		if ply:IsAdmin() then
			gSAM.SetPlayerGroup(ply,GSAM_GROUP_ADMIN)
		elseif ply:IsSuperAdmin() then
			gSAM.SetPlayerGroup(ply,GSAM_GROUP_HEADADMIN)
		else
			gSAM.SetPlayerGroup(ply,GSAM_GROUP_PLAYER)
		end
		gSAM.CreatePlayerVar(ply:SteamID(),"FirstJoined",os.date("%d.%m.%y"))
	end
	local times = gSAM.GetPlayerVar(ply:SteamID(),"TimesJoined") or 0
	for k,v in pairs(player.GetAll()) do
		v:ChatPrint("Player "..ply:Name().."("..ply:SteamID()..") has joined! He/She has been here "..times)
	end
	if !ftime then
		gSAM.SetPlayerVar(ply:SteamID(),"TimesJoined",times + 1)
	end
	ply:SetTeam(gSAM.GetPlayerGroup(ply))
	ply:ChatPrint("You are in the "..team.GetName(gSAM.GetPlayerGroup(ply)).." User Group!")
	if gSAM.Settings.MOTD then
		if !(gSAM.GetPlayerGroup(ply) <= GSAM_GROUP_ADMIN) then
			ply:ConCommand("gSAM_Open_MOTD")
		end
	end
end
hook.Add("PlayerInitialSpawn","gSAM.PlayerInitSpawn",gSAM.PlayerInitSpawn)
